<template>
  <div class="product-detail">
    <s-header name="商品详情"></s-header>
    <!-- 内容区域 -->
    <div class="detail-content">
      <div class="product-swipe-wrap">
        <!-- 商品轮播图区域 -->
        <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
          <van-swipe-item v-for="(item, index) in state.detail.goodsCarouselList" :key="index">
            <img :src="item" alt="" />
          </van-swipe-item>
        </van-swipe>
      </div>
      <div class="product-info">
        <div class="product-title">
          {{ state.detail.goodsName || "商品名称" }}
        </div>
        <div class="product-desc">免邮费，顺丰快递</div>
        <div class="product-price">
          <span>￥{{ state.detail.sellingPrice || "" }}</span>
        </div>
      </div>
      <div class="product-intro">
        <ul>
          <li>概述</li>
          <li>参数</li>
          <li>安装服务</li>
          <li>常见问题</li>
        </ul>
        <div class="product-content" v-html="state.detail.goodsDetailContent"></div>
      </div>
    </div>
    <!-- 动作栏 -->
    <van-action-bar>
      <van-action-bar-icon icon="chat-o" text="客服" @click="aaa" />
      <van-action-bar-icon icon="cart-o" text="购物车" :badge="!count ? '' : count" @click="goTo" />
      <van-action-bar-button type="warning" text="加入购物车" @click="handleAddCart" />
      <van-action-bar-button type="danger" text="立即购买" @click="goToCart" />
    </van-action-bar>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, reactive } from "vue";
import { showToast } from "vant";
import { useRoute, useRouter } from "vue-router";
import sHeader from "@/components/SimpleHeader.vue";
import { getDetail } from "@/service/good";
import { addCart } from "@/service/cart.js";
import { useCartStore } from "@/stores/cart.js";
import { getCart } from "@/service/cart";
import { storeToRefs } from "pinia"; // 引入storeToRefs，用于解构pinia中的数据

import { prefix } from "@/common/js/utils.js";
const route = useRoute();
const router = useRouter();
const id = route.query.id; // 商品id
const cartStore = useCartStore(); // 购物车状态管理
const { count } = storeToRefs(cartStore); // 解构pinia中的数据

// 商品详情数据
const state = reactive({
  detail: {
    goodsCarouselList: [], // 商品轮播图列表
  },
});
onMounted(async () => {
  const { data } = await getDetail(id);
  data.goodsCarouselList = data.goodsCarouselList.map((i) => prefix(i)); // 图片前缀处理
  state.detail = data; // 商品详情数据
  cartStore.updateCart(); // 每次进入详情页的时候，默认获取一次最新购物车状态数据
  // console.log(state.detail);
});
// 加入购物车
const handleAddCart = async () => {
  const res = await addCart({ goodsCount: 1, goodsId: state.detail.goodsId });
  // console.log(res, "CARD");

  if (res.resultCode === 200) {
    showToast("加入购物车成功");
  }
  cartStore.updateCart(); // 更新购物车数据
};
// 立即购买
const goToCart = async () => {
  //购买商品也是调用添加购物车接口，然后跳转到购物车页面 并更新购物车数据
  await addCart({ goodsCount: 1, goodsId: state.detail.goodsId });
  router.push({ path: "/cart" });
  cartStore.updateCart(); // 更新购物车数据
};
// 前往购物车页面
const goTo = () => {
  router.push({ path: "/cart" });
};
const aaa = async () => {
  showToast("客服功能暂未开放");
};
</script>
<style lang="less">
@import "@/common/style/mixin.less";
.product-detail {
  .detail-content {
    height: calc(100vh - 50px); // 减去头部高度
    overflow: hidden; // 超出隐藏
    overflow-y: auto; // 开启垂直滚动条
    .product-swipe-wrap {
      .van-swipe {
        height: 350px;
        color: #fff;
        font-size: 20px;
        text-align: center;
        background-color: #fff;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    .product-info {
      padding: 10px;
      padding-bottom: 0px;
      .product-title {
        font-size: 18px;
        color: #333;
        margin-bottom: 5px;
      }
      .product-desc {
        font-size: 14px;
        color: #999;
        margin-bottom: 0px;
        text-align: left;
      }
      .product-price {
        font-size: 20px;
        color: #f63515;
      }
    }
    .product-intro {
      width: 100%;
      padding-bottom: 50px;
      ul {
        display: flex;
        justify-content: space-around;
        width: 100%;

        li {
          flex: 1; // 均分
          padding: 5px 0;
          text-align: center;
          font-size: 15px;
          border-right: 1px solid #999;
          box-sizing: border-box;
          &:last-child {
            //最后一个元素去掉右边框
            border-right: none;
          }
        }
      }

      .product-content {
        padding: 0 20px;
        img {
          width: 100%;
        }
      }
    }
  }
  .van-action-bar-button--warning {
    background: linear-gradient(to right, #6bd8d8, @primary);
  }
  .van-action-bar-button--danger {
    background: linear-gradient(to right, #0dc3c3, @primary);
  }
}
</style>
